{
 "metadata": {
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.5"
  },
  "orig_nbformat": 2,
  "kernelspec": {
   "name": "python385jvsc74a57bd04c36a90a135e4aa4256adc3692917ccc380701b533777f3da43dc8bc5264a29c",
   "display_name": "Python 3.8.5 64-bit ('base': conda)"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2,
 "cells": [
  {
   "source": [
    "# 第一次作业第6题\n",
    "作者：赵飞\n",
    "学号：202018019427078\n",
    "日期：2021-04-18"
   ],
   "cell_type": "markdown",
   "metadata": {}
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "output_type": "stream",
     "name": "stdout",
     "text": [
      "方程的解为： [1.75 1.75 0.75]\n解x满足方程\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "# 方程系数矩阵A\n",
    "A = np.array([\n",
    "    [3, 6, -5],\n",
    "    [1, -3, 2],\n",
    "    [5, -1, 4]\n",
    "])\n",
    "# 常数向量b\n",
    "b = np.array([12, -2, 10])\n",
    "# 检验解是否存在及唯一\n",
    "if np.linalg.det(A) == 0:\n",
    "    # 解很多，求最小二乘解\n",
    "    x = np.linalg.lstsq(A, b)\n",
    "else:\n",
    "    x = np.linalg.solve(A, b)\n",
    "print(\"方程的解为：\", x)\n",
    "# 验证解\n",
    "if np.all(A.dot(x)==b) == True:\n",
    "    print(\"解x满足方程\")\n",
    "else:\n",
    "    print(\"解x不满足方程\")"
   ]
  }
 ]
}